<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<%@ include file="/include.jsp"%>

<script type="text/javascript">
var menuId = "${menuId}";
$(function() {
    var $datagrid = $('#datagrid');
    var $querypanel = $('#querypanel');
    var $querybtn = $('#querybtn');
    var $contextmenu = $('#contextmenu');
    var $addwindow = $('#addwindow');
    var $updatewindow = $('#updatewindow');
    var $assignrolewindow = $('#assignrolewindow');
    var $userauthwindow = $('#userauthwindow');
    var $viewauthwindow = $('#viewauthwindow');
    
    var handlers = {
        'view': function() {
            alert("查看");
        },
        
        'add': function() {
            $addwindow.panel('open');
        },
        
        'update': function() {
            $updatewindow.panel('open');
        },
        
        'delete': function() {
            var user = $datagrid.datagrid('getSelected');
            if (user) {
                $.getJSON(ctx + '/sys/user/delete', {id: user.id}, function(json) {
                    $datagrid.datagrid('getPager').pagination('select');
                }, 'json');
            }
        },
        
        'assignrole': function() {
            $assignrolewindow.panel('open');
        },
        
        'userauth': function() {
            $userauthwindow.panel('open');
        },
        
        'viewauth': function() {
            $viewauthwindow.panel('open');
        }
    };
    
    $datagrid.datagrid({
        title: '用户列表',
        iconCls: 'icon-edit',
        columns: [[
            {field: 'id', title: '用户ID', sortable: true, width: 100},
            {field: 'loginAct', title: '登录名', sortable: true, width: 100},
            {field: 'loginPwd', title: '密码', sortable: true, width: 100},
            {field: 'name', title: '人员名', sortable: true, width: 100}
        ]],
        idField: 'id',
        rownumbers: true,
        fitColumns: true,
        collapsible: true,
        singleSelect: true,
        selectOnCheck: true,
        checkOnSelect: true,
        pagination: true,
        remoteSort: false,
        onRowContextMenu: function(e, rowIndex, rowData) {
            e.preventDefault();
            $datagrid.datagrid('clearSelections').datagrid('selectRow', rowIndex);
            $contextmenu.menu('show', {
                left: e.pageX - 10,
                top: e.pageY - 10
            });
        },
        onBeforeLoad: function(param) {
            $.getJSON(ctx + '/sys/oper/list-authorized-opers-by-menuid', {
                menuId: menuId
            }, function(opers) {
            	var btns = [];
            	var items = [];
                $.each(opers, function(i, oper){
                	if (i > 0) {
                		btns.push('-');
                		items.push({separator:true});
                	}
                	btns.push({
                        id: 'btn-' + oper.id,
                        text: oper.name,
                        iconCls: oper.iconCls,
                        handler: handlers[oper.handler],
                    });
                	items.push({
                	    text: oper.name,
                        iconCls: oper.iconCls,
                        onclick: handlers[oper.handler]
                	});
                });
                $datagrid.datagrid("appendToolbarItem", btns);
                
                $.each(items, function(i, item) {
                    $contextmenu.menu('appendItem', item);
                });
            });
        },
        toolbar: []
    }).datagrid('getPager').pagination({
        total: 0,
        pageSize: 10,
        pageNumber: 1,
        pageList: [5, 10, 15],
        beforePageText: '第',
        afterPageText: '页&nbsp;共{pages}页',
        displayMsg: '当前:{from}-{to}/总数:{total}',
        onSelectPage: function(pageNumber, pageSize) {
            var pager = $.getPager($querypanel);
            pager.pageNumber = pageNumber;
            pager.pageSize = pageSize;
            var url = ctx + '/sys/user/pager';
            $.postJSON(url, pager, function(pager) {
                $datagrid.datagrid('loadData', pager.entities);
                $datagrid.datagrid('getPager').pagination('refresh', {
                    total: pager.total,
                    pageNumber: pager.pageNumber
                });
            }, 'json');
        }
    }).pagination('select');
    
    $querypanel.panel({
    	title: '查询用户',
    	iconCls: 'icon-search',
    	collapsible: true
    }).panel('collapse');
    
    $querybtn.on('click', function(e) {
    	e.preventDefault();
    	$datagrid.datagrid('getPager').pagination('select', 1);
    });
    
    $contextmenu.menu({}).menu('hide');
    
    $addwindow.dialog({
        title: '添加用户',
        width: 400,
        height: 300,
        collapsible: true,
        closed: true,
        cache: false,
        modal: true,
        href: ctx + '/sys/user/add',
        method: 'get',
        buttons: [{
            text: '保存', iconCls: 'icon-save', handler: function() {
            	var $form = $addwindow.find('form');
            	$form.form({
            		url: $form.attr('action') || ctx + '/sys/user/add',
            		success: function(jsonResult) {
            		    jsonResult = eval('(' + jsonResult + ')');
            		    if (jsonResult.success) {
            		        $addwindow.window('close');
            			    $datagrid.datagrid('getPager').pagination('select');
            		    }
            		}
            	}).submit();
            }
        }]
    });
    
    $updatewindow.dialog({
        title: '修改用户',
        width: 400,
        height: 300,
        collapsible: true,
        closed: true,
        cache: false,
        modal: true,
        href: ctx + '/sys/user/update',
        method: 'get',
        onBeforeOpen: function() {
        	var user = $datagrid.datagrid('getSelected');
            if (user) {
            	var params = $updatewindow.panel('options').queryParams;
            	$.extend(params, {
            		userId: user.id
            	});
            	$updatewindow.panel('options').queryParams = params;
            } else {
                return false;
            }
        },
        buttons: [{
            text: '保存', iconCls: 'icon-save', handler: function() {
                var $form = $updatewindow.find('form');
                $form.form({
                    url: $form.attr('action') || ctx + '/sys/user/update',
                    success: function(jsonResult) {
                        jsonResult = eval('(' + jsonResult + ')');
                        if (jsonResult.success) {
                            $updatewindow.window('close');
                            $datagrid.datagrid('getPager').pagination('select');
                        }
                    }
                }).submit();
            }
        }]
    });
    
    $assignrolewindow.dialog({
        title: '分配角色',
        width: 600,
        height: 400,
        maximizable: true,
        collapsible: true,
        closed: true,
        cache: false,
        modal: true,
        href: ctx + '/sys/user/assignrole',
        method: 'get',
        onBeforeOpen: function() {
            var user = $datagrid.datagrid('getSelected');
            if (user) {
                var params = $assignrolewindow.panel('options').queryParams;
                $.extend(params, {
                    userId: user.id
                });
                $assignrolewindow.panel('options').queryParams = params;
            } else {
                return false;
            }
        },
        buttons: [{
            text: '保存', iconCls: 'icon-save', handler: function() {
                var url = ctx + '/sys/user/assignrole';
                var $assignrolegrid = $('#assignrolegrid');
                var user = $datagrid.datagrid('getSelected');
                var roles = $assignrolegrid.datagrid('getSelections');
                var userroles = {
                    userId: user.id,
                    roleIds: []
                };
                $.each(roles, function(i, role) {
                    userroles.roleIds.push(role.id);
                });
                $.post(url, userroles, function(json) {
                    if (json.success) {
                        $assignrolewindow.window('close');
                    }
                }, 'json');
            }
        }]
    });
    
    $userauthwindow.dialog({
        title: '用户授权',
        width: 300,
        height: 500,
        maximizable: true,
        collapsible: true,
        closed: true,
        cache: false,
        modal: true,
        href: ctx + '/sys/user/userauth',
        method: 'get',
        onBeforeOpen: function() {
            var user = $datagrid.datagrid('getSelected');
            if (user) {
                var params = $userauthwindow.panel('options').queryParams;
                $.extend(params, {
                    userId: user.id
                });
                $userauthwindow.panel('options').queryParams = params;
            } else {
                return false;
            }
        },
        buttons: [{
            text: '保存', iconCls: 'icon-save', handler: function() {
                var url = ctx + '/sys/user/userauth';
                var $userauthtree = $("#userauthtree");
                var user = $datagrid.datagrid('getSelected');
                var nodes = $userauthtree.tree('getChecked', ['checked', 'indeterminate']);
                var acls = {
                    userId: user.id,
                    menuIds: [],
                    operIds: [],
                };
                $.each(nodes, function(i, node){
                    if ('menu' == node.resourceType) {
                        acls.menuIds.push(node.id);
                    } else if('oper' == node.resourceType) {
                        acls.operIds.push(node.id);
                    }
                });
                $.post(url, acls, function(json) {
                    if (json.success) {
                        $userauthwindow.window('close');
                    }
                }, 'json');
            }
        }]
    });
    
    $viewauthwindow.dialog({
        title: '查看授权',
        width: 300,
        height: 500,
        maximizable: true,
        collapsible: true,
        closed: true,
        cache: false,
        modal: true,
        href: ctx + '/sys/user/viewauth',
        method: 'get',
        onBeforeOpen: function() {
            var user = $datagrid.datagrid('getSelected');
            if (user) {
                var params = $viewauthwindow.panel('options').queryParams;
                $.extend(params, {
                    userId: user.id
                });
                $viewauthwindow.panel('options').queryParams = params;
            } else {
                return false;
            }
        }
    });
    
});
</script>

<title>TSWEB</title>
</head>
<body>
    <div id="querypanel" style="width:100%;padding:5px;">
        <form action="javascript:void(0)" method="post">
	        <div>
	            <select class="easyui-combobox" name="relation">
	                <option value="and">And</option>
	                <option value="or">Or</option>
	            </select>
	            <select class="easyui-combobox" name="condition">
	                <option value=""></option>
	                <option value="user_id">用户ID</option>
	                <option value="user_login_act">登录名</option>
	                <option value="user_login_pwd">密码</option>
	            </select>
	            <select class="easyui-combobox" name="operator">
	                <option value="eq">=</option>
	                <option value="gt">&gt;</option>
	                <option value="ge">&gt;=</option>
	                <option value="lt">&lt;</option>
	                <option value="le">&lt;=</option>
	                <option value="ne">!=</option>
	                <option value="like">like</option>
	                <option value="notlike">not like</option>
	            </select>
	            <input class="easyui-textbox" name="value" value=""/>
	        </div>
	        <div>
	            <select class="easyui-combobox" name="relation">
	                <option value="and">And</option>
	                <option value="or">Or</option>
	            </select>
	            <select class="easyui-combobox" name="condition">
	                <option value=""></option>
                    <option value="user_id">用户ID</option>
                    <option value="user_login_act">登录名</option>
                    <option value="user_login_pwd">密码</option>
	            </select>
	            <select class="easyui-combobox" name="operator">
	                <option value="eq">=</option>
	                <option value="gt">&gt;</option>
	                <option value="ge">&gt;=</option>
	                <option value="lt">&lt;</option>
	                <option value="le">&lt;=</option>
	                <option value="ne">!=</option>
	                <option value="like">like</option>
	                <option value="notlike">not like</option>
	            </select>
	            <input class="easyui-textbox" name="value" value=""/>
	        </div>
	        <div>
	            <select class="easyui-combobox" name="relation">
	                <option value="and">And</option>
	                <option value="or">Or</option>
	            </select>
	            <select class="easyui-combobox" name="condition">
	                <option value=""></option>
                    <option value="user_id">用户ID</option>
                    <option value="user_login_act">登录名</option>
                    <option value="user_login_pwd">密码</option>
	            </select>
	            <select class="easyui-combobox" name="operator">
	                <option value="eq">=</option>
	                <option value="gt">&gt;</option>
	                <option value="ge">&gt;=</option>
	                <option value="lt">&lt;</option>
	                <option value="le">&lt;=</option>
	                <option value="ne">!=</option>
	                <option value="like">like</option>
	                <option value="notlike">not like</option>
	            </select>
	            <input class="easyui-textbox" name="value" value=""/>
	        </div>
	        <div>
	           <a href="javascript:void(0)" id="querybtn" class="easyui-linkbutton"
	               data-options="iconCls:'icon-search'"
	               style="margin-top:5px;">查询</a>
	        </div>
        </form>
    </div>
    <div style="margin:5px 0;"></div>
    <table id="datagrid" style="width:100%;"></table>
    <div id="contextmenu"></div>
    <div id="addwindow"></div>
    <div id="updatewindow"></div>
    <div id="assignrolewindow"></div>
    <div id="userauthwindow"></div>
    <div id="viewauthwindow"></div>
</body>
</html>